%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%This is part of the set of files that accompany the article:       %
%Mankiw, N. Gregory and Ricardo Reis (2007) "Sticky Information in  %
%General Equilibrium," Journal of the European Economic Association,%
%forthcoming. See the appendix of the NBER or CEPR working paper    %
%versions for a detailed explanation of the algorithms.             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Please cite if you use the programs. I do not provide tech support.%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Last revised: August 30, 2006                                      %
%Written by: Ricardo Reis                                           %
%Input: File with Bayesian draws Bayes.mat                          %
%Output: Variance decompositions Bayesvd.mat                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%% STEP1: Variance decomposition for the median Bayesian estimates %%%%
clear; clc; global N; N=206;
load Bayes.mat;
x=pars_c; theta=median(MixedResults);
x(4)=theta(1); x(5)=theta(2); x(12)=theta(3); x(13)=theta(4);
x(14)=theta(5); x(15)=theta(6); x(16)=theta(7); x(17)=theta(8);
x(18)=theta(9); x(19)=theta(10); x(20)=theta(11);
VD_med=SIGEvardec(x);

%%%% STEP2: Variance decomposition for Num draws %%%%
VARDEC=[]; Num=length(MixedResults);
h = waitbar(0,'Going over variance decompositions...');
for i=1:Num
    theta=MixedResults(i,:);
    x(4)=theta(1); x(5)=theta(2); x(12)=theta(3); x(13)=theta(4);
    x(14)=theta(5); x(15)=theta(6); x(16)=theta(7); x(17)=theta(8);
    x(18)=theta(9); x(19)=theta(10); x(20)=theta(11);
    VARDEC=[VARDEC SIGEvardec(x)];
    waitbar(i/Num,h);
end
close(h);
for i=1:5
    for j=1:5
        for k=1:Num
            z(i,j,k)=VARDEC(i,j+5*(k-1));
        end
    VD_025(i,j)=prctile(z(i,j,:),2.5);
    VD_500(i,j)=prctile(z(i,j,:),50);
    VD_975(i,j)=prctile(z(i,j,:),97.5);
    end
end

clear Num VARDEC i j k pars_c theta x h MixedResults  R accept_rate burnin cc
save Bayesvd.mat